<?php
namespace Helper\Controller;

use Common\Controller\ApiController;

class NotificationController extends ApiController
{
    public function getNotifications()
    {
        $user = $this->user;
        $input = $this->request();
        $page = filter($input['page'], 1);
        $perPage = filter($input['per_page'], 10);
        $perPage > 30 && $perPage = 30;
        $where = [];
        $where['is_hidden'] = 'F';
        $where['user_id'] = $user['id'];
        $count = D('Notification')->where($where)->count();
        $notifications = D('Notification')
                ->where($where)
                ->page($page, $perPage)
                ->order('created_at desc')
                ->select();
        // 全部标记为已读
        if ($notifications) D('Notification')->where(['user_id' => $user['id'], 'is_read' => 'F'])->setField('is_read', 'T');

        $notifications = transform('Notification', 'listview', $notifications);
        $pagination = pagination($count, $perPage, $page);
        $notifications = $notifications ? : [];
        $this->response($notifications, $pagination);
    }
}
